package cn.green.mapper.model.impls.field;

import cn.green.mapper.model.FieldColumnMapper;
import cn.green.mvc.entity.OrmTableEntity;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author wang.yishu
 * @version 1.0
 * @className DynamicFieldColumnMapper
 * @description 动态字段映射
 * @date 2025/7/23 10:22
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DynamicFieldColumnMapper extends FieldColumnMapper {

    @Override
    public void setFieldValue(JSONObject row, OrmTableEntity entity) throws Exception {
        if (entity == null) {
            return;
        }
        JSONObject data = entity.getData();
        if (data == null) {
            data = new JSONObject();
        }
        String fieldName = this.column.getFieldName();
        if (row != null && row.containsKey(fieldName)) {
            data.put(fieldName, row.get(fieldName));
        }
    }

    @Override
    public void setColumnValue(JSONObject row, OrmTableEntity entity) throws Exception {
        if (entity == null) {
            return;
        }
        JSONObject data = entity.getData();
        if (data == null) {
            return;
        }
        String fieldName = this.column.getFieldName();
        row.put(fieldName, data.get(fieldName));
    }
}
